package com.example.test.controller;


import com.alibaba.fastjson.JSONObject;
import com.example.test.bean.DTO.input.GoodsCreateInputDTO;
import com.example.test.bean.DTO.input.GoodsUpdateInputDTO;
import com.example.test.common.model.PageModel;
import com.example.test.common.model.R;
import com.example.test.common.token.LoginToken;
import com.example.test.service.GoodsService;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * <p>
 * 商品信息表 前端控制器
 * </p>
 *
 * @author dz
 * @since 2022-04-10
 */
@CrossOrigin
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    private GoodsService goodsService;

    @LoginToken
    @ApiOperation(value = "商品信息表根据keyword分页查询")
    @PostMapping("/getGoods")
    public R pageList(@RequestBody JSONObject jsonParam) {
//        public R pageList(@RequestBody JSONObject jsonParam) {
        System.out.println(jsonParam.toJSONString());
        String keyword = jsonParam.getString("keyword");
        Long size = Long.valueOf(jsonParam.getString("size"));
        Long current = Long.valueOf(jsonParam.getString("current"));
        PageModel pageModel = new PageModel();
        pageModel.setCurrent(current);
        pageModel.setSize(size);
        return goodsService.getGoodsByKeyword(keyword,pageModel);
    }
    @LoginToken
    @ApiOperation(value = "商品信息表分页查询")
    @GetMapping("/getAllGoods")
    public R getAllGoods(PageModel pageModel) {
        return goodsService.getAllGoods(pageModel);
    }
    @LoginToken
    @ApiOperation(value = "商品信息id查询")
    @PostMapping("/getGoodsById")
    public R getGoodsById(@RequestBody JSONObject jsonParam) {
        System.out.println(jsonParam.toJSONString());
        Long goodsId = jsonParam.getLong("goodsId");
        return goodsService.getGoodsById(goodsId);
    }
    @LoginToken
    @ApiOperation(value = "添加")
    @PostMapping(value = "/insert")
    public R insert(@RequestBody JSONObject jsonParam) {
        //public R insert(@Validated @ApiParam  GoodsCreateInputDTO goodsCreateInputDTO) {
        System.out.println(jsonParam.toJSONString());
        GoodsCreateInputDTO goodsCreateInputDTO = new GoodsCreateInputDTO();
        goodsCreateInputDTO.setGoodsName(jsonParam.getString("goodsName"));
        goodsCreateInputDTO.setBraendName(jsonParam.getString("braendName"));
        goodsCreateInputDTO.setTypeName(jsonParam.getString("typeName"));
        goodsCreateInputDTO.setGoodsRemain(Long.valueOf(jsonParam.getString("goodsRemain")));
        goodsCreateInputDTO.setSellPrice(jsonParam.getDouble("sellPrice"));
        goodsCreateInputDTO.setPurchasePrice(jsonParam.getDouble("purchasePrice"));
        return goodsService.addGoods(goodsCreateInputDTO);
    }
    @LoginToken
    @ApiOperation(value = "修改")
    @PostMapping(value = "/update")
    public R update(@RequestBody JSONObject jsonParam) {
//        public R update(@Validated @ApiParam GoodsUpdateInputDTO goodsUpdateInputDTO) {
        System.out.println(jsonParam.toJSONString());
        GoodsUpdateInputDTO goodsUpdateInputDTO = new GoodsUpdateInputDTO();
        goodsUpdateInputDTO.setGoodsId(Long.valueOf(jsonParam.getString("goodsId")));
        goodsUpdateInputDTO.setGoodsName(jsonParam.getString("goodsName"));
        goodsUpdateInputDTO.setBraendName(jsonParam.getString("braendName"));
        goodsUpdateInputDTO.setTypeName(jsonParam.getString("typeName"));
        goodsUpdateInputDTO.setGoodsRemain(Long.valueOf(jsonParam.getString("goodsRemain")));
        goodsUpdateInputDTO.setSellPrice(jsonParam.getDouble("sellPrice"));
        goodsUpdateInputDTO.setPurchasePrice(jsonParam.getDouble("purchasePrice"));
        return goodsService.updateGoods(goodsUpdateInputDTO);
    }
    @LoginToken
    @ApiOperation(value = "删除")
    @PostMapping(value = "/delete")
    public R<Boolean> delete(@RequestBody JSONObject jsonParam) {
        System.out.println(jsonParam.toJSONString());
        Long goodsId = jsonParam.getLong("goodsId");
        return goodsService.deleteGoods(goodsId);
    }

}

